package com.tancire.sources.controller;

import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.page.TableDataInfo;
import com.tancire.common.exception.ResourceException;
import com.tancire.sources.domain.*;
import com.tancire.sources.service.ISurResourceCategoryService;
import com.tancire.sources.service.ISurResourceDetailService;
import com.tancire.sources.service.ISurResourceService;
import com.tancire.sources.service.ISurTagsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资源管理Controller
 *
 * @author chenchao
 * @date 2024-07-05
 */
@RestController
@RequestMapping("/open/client/sources")
public class SurResourceOpenController extends BaseController {

    @Resource
    private ISurResourceService surResourceService;

    @Resource
    private ISurResourceDetailService surResourceDetailService;

    @Resource
    private ISurResourceCategoryService surResourceCategoryService;

    @Resource
    private ISurTagsService surTagsService;

    /**
     * 查询资源分类列表
     */
    @GetMapping("/category/main/list")
    public AjaxResult listMainCategory() {
        List<SurResourceCategory> list = surResourceCategoryService.listMainCategory();
        return AjaxResult.success(list);
    }

    /**
     * 查询资源分类列表
     */
    @GetMapping("/category/list/{pid}")
    public AjaxResult listCategoryByPid(@PathVariable("pid") Long pid) {
        List<SurResourceCategory> list = surResourceCategoryService.listCategoryByPid(pid);
        return AjaxResult.success(list);
    }

    /**
     * 查询资源标签列表
     */
    @GetMapping("/tag/list/{type}")
    public AjaxResult listTag(@PathVariable String type) {
        List<SurTags> list = surTagsService.selectSurTagsListByType(type);
        return AjaxResult.success(list);
    }

    /**
     * 获取资源管理详细信息
     */
    @GetMapping(value = "/info/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(surResourceService.selectSurResourceById(id));
    }

    /**
     * 查询资源管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SurResource surResource) {
        startPage();
        List<SurResource> list = surResourceService.selectSurResourceList(surResource);
        return getDataTable(list);
    }

    /**
     * 获取观看视频授权码
     * 开放接口，不需要登录
     */
    @GetMapping("/view/code/{id}")
    public AjaxResult getViewCode(@PathVariable("id") Long id) {
        SourceCode code = new SourceCode();
        try {
            code = surResourceDetailService.getViewCode(id, true);
        } catch (Exception e) {
            code.setAuthStatus(3);
            code.setMsg("获取授权码错误");
            return AjaxResult.success(code);
        }
        return AjaxResult.success(code);
    }

    /**
     * 视频预览接口
     *
     * @param code
     * @param req
     * @param res
     * @return
     */
    @RequestMapping("/video/view/{code}")
    public void view(@PathVariable("code") String code, HttpServletRequest req, HttpServletResponse res) {
        // 从redis取资源
        try {
            this.surResourceDetailService.view(code, req, res);
        } catch (Exception e) {
            throw new ResourceException(e.getMessage());
        }
    }

    /**
     * 视频预览接口
     *
     * @param code
     * @param req
     * @param res
     * @return
     */
    @RequestMapping("/common/download/{code}")
    public void download(@PathVariable("code") String code, HttpServletRequest req, HttpServletResponse res) {
        // 从redis取资源
        try {
            this.surResourceDetailService.download(code, req, res);
        } catch (Exception e) {
            throw new ResourceException(e.getMessage());
        }
    }

    /**
     * 查询资源明细列表
     */
    @GetMapping("/detail/list/{resourceId}")
    public AjaxResult listByResourceId(@PathVariable("resourceId") Long resourceId) {
        List<SurResourceDetail> list = surResourceDetailService.listByResourceId(resourceId);
        return AjaxResult.success(list);
    }

}
